/* uart.c */

#include <avr/io.h>
#include <inttypes.h>

#include "uart.h"

char uartGetChar(void)
{
	while(!(UCSRA&(1<<RXC)));	// Wait for char received
	return UDR;					// Get char
}
	
void uartSendChar(char x)
{
	while(!(UCSRA&(1<<UDRE)));	// Wait for UDR empty
	UDR = x;					// Send char
}

void uartInit(void)
{
	UBRRH = 0;
	//UBRRH = 1;
	//UBRRL = 0xA0;
	UBRRL = UBRR_VAL; 					// 9600 baud
	UCSRB = (1<<TXEN)|(1<<RXEN);	// Enable Tx and Rx
}

void uartGetString(char* buf)
{
	char* cptr;
	char x;	
	for(cptr = buf; x != CR; cptr++)
	{
		x = uartGetChar();
		if(x != CR) 				// If CR do not send it or store it to buffer
		{
			uartSendChar(x);
			*cptr = x;
		}
	}
	*(--cptr) = NULL;					// Store NULL at end of string
	uartSendChar(CR);
	uartSendChar(LF);
	uartSendChar(PROMPT);
}

void uartSendString(char* buf)
{
	char* cptr;
	for(cptr= buf; *cptr != 0; cptr++)
	{
		uartSendChar(*cptr);
	}
	uartSendChar(CR);
	uartSendChar(LF);
	//uartSendChar(PROMPT);
}

void customChar2(void)
{
	uartSendChar(250); // Define 3rd Custom Character
	uartSendChar(0); //%00000
	uartSendChar(4); //%00100 *
	uartSendChar(14); //%01110 ***
	uartSendChar(31); //%11111 *****
	uartSendChar(4); //%00100 *
	uartSendChar(4); //%00100 *
	uartSendChar(4); //%00100 *
	uartSendChar(4); //%00100 *
}
	

	
	
